package com.cms.recursoshumanos.container;

import javax.microedition.global.Formatter;

import com.cms.recursoshumanos.object.ArticuloCarrito;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

public class CustomHorizontalFieldManager extends HorizontalFieldManager
{
	int background_color = 0;
	LabelField lbl_nombre, lbl_cant, lbl_precio;
	
	public int hfm_cantidadWidth = getFont().getAdvance("0000");
	public int hfm_precioWidth = getFont().getAdvance("0000000.00") - 5;	
	public int hfm_nombreWidth = WIDTH_HFM - hfm_cantidadWidth - hfm_precioWidth - (WIDTH_HFM*8)/100;
	
	int hfm_cantidadHeight = 20;
	int hfm_precioHeight = 20;
	int hfm_nombreHeight = 20;

	private static final int WIDTH = Display.getWidth();
	private static final int WIDTH_HFM = WIDTH*(80)/100;
	
	protected ArticuloCarrito elArticuloCarrito;
	protected int _drawPosition = -1;
	protected boolean _isFocus = false;
	
	protected int _targetheight = 0;
	
	protected XYEdges paddin_label = new XYEdges(1, 1, 1, 1);
	
	private static final int COLOR_BACKGROUND = 0xFFFFFF;
	protected static final int COLOR_BORDER = 0xBBBBBB;
	protected static int COLOR_BACKGROUND_FOCUS = 0xE4E4E4;
	protected static int COLOR_BACKGROUND_FOCUS_ACTION = 0x186DEF;
	
	protected static final int CORNER_RADIUS = 18;
	public static int SEPARATOR_LONG = 20;
	protected int _preferedWidth = 100;
	
	public CustomHorizontalFieldManager(ArticuloCarrito elArticuloCarrito,long style)
	{
		super(style);
		this.elArticuloCarrito = elArticuloCarrito;
		calcular_heghts();
		setComponents();
	}
	public ArticuloCarrito getArticuloCarrito()
	{
		return this.elArticuloCarrito;
	}
	public int getNombreWidth()
	{
		return this.hfm_nombreWidth;
	}
	public int getPrecioWidth()
	{
		return this.hfm_precioWidth;
	}
	public int getCantidadWidth()
	{
		return this.hfm_cantidadWidth;
	}
	public void calcular_heghts()
	{
		
		int nombreWidth = getFont().getAdvance(elArticuloCarrito.getArticulo().getNombreArticulo());
		
		int nombreHeight = getFont().getHeight();
		//int nombreHeight = getFont().getHeight();
		  
		if( nombreWidth > hfm_nombreWidth  ) 
		{
			int mod_width = nombreWidth%hfm_nombreWidth;
			int div_width = nombreWidth/hfm_nombreWidth;
			
			if (mod_width != 0)
			{
				div_width = div_width + 1;
			}
			nombreHeight = (nombreHeight * (div_width));
			
		}
		//hfm_nombreWidth = nombreWidth + HPADDING * 2;
		_targetheight = nombreHeight; //+ VPADDING*2;
		/*setExtent( WIDTH_HFM, 
				lbl_nombre.getPreferredHeight());*/
	}
	public void setComponents()
	{
		Border elBorderLabel = BorderFactory.createSimpleBorder(new  XYEdges(0, 1, 0, 0));
		
		
		lbl_cant = new LabelField(String.valueOf(this.elArticuloCarrito.getCantidadSeleccionada()), 
				DrawStyle.LEFT | FIELD_VCENTER | FIELD_HCENTER | USE_ALL_WIDTH | USE_ALL_HEIGHT)
		{
			

			public int getPreferredHeight() 
			{
				
				return _targetheight;
			}
			
			public int getPreferredWidth() 
			{
				return hfm_cantidadWidth;
			}
			protected void applyTheme(Graphics g, boolean arg1) 
			{
				g.setColor(Color.BLACK);
				super.applyTheme(g, arg1);
			}
			protected void layout(int width, int height) 
			{
				super.layout(width, height);
				setExtent(this.getPreferredWidth(), this.getPreferredHeight());
			}
		};
		float value_cal = this.elArticuloCarrito.getArticulo().getPrecioUnitario()*
				this.elArticuloCarrito.getCantidadSeleccionada();
		Formatter f = new Formatter();
		String str_val = f.formatNumber(value_cal,2);
		lbl_precio = new LabelField(str_val, 
				DrawStyle.RIGHT | FIELD_VCENTER | FIELD_HCENTER | USE_ALL_WIDTH | USE_ALL_HEIGHT)
		{
			
			
			public int getPreferredHeight() 
			{
				return _targetheight;
			}
			public int getPreferredWidth() 
			{
				return hfm_precioWidth;
			}
			protected void applyTheme(Graphics g, boolean arg1) 
			{
				g.setColor(Color.BLACK);
				super.applyTheme(g, arg1);
			}
			protected void layout(int width, int height) 
			{
				super.layout(width, height);
				setExtent(this.getPreferredWidth(), this.getPreferredHeight());
			}
			
		};
		
		lbl_nombre = new LabelField(this.elArticuloCarrito.getArticulo().getNombreArticulo(), 
				DrawStyle.LEFT | FIELD_VCENTER | FIELD_HCENTER | USE_ALL_WIDTH | USE_ALL_HEIGHT)
		{
			
			
			public int getPreferredHeight() 
			{
				 return _targetheight;
			}
			
			public int getPreferredWidth() 
			{
				return hfm_nombreWidth;
			}
			protected void applyTheme(Graphics g, boolean arg1) 
			{
				g.setColor(Color.BLACK);
				super.applyTheme(g, arg1);
			}
			protected void layout(int width, int height) 
			{
				super.layout(width, height);
				setExtent(this.getPreferredWidth(), this.getPreferredHeight());
			}
			
		};
		
		
		lbl_nombre.setMargin(paddin_label);
		lbl_precio.setMargin(paddin_label);
		lbl_cant.setMargin(paddin_label);
		lbl_nombre.setBorder(elBorderLabel);
		lbl_cant.setBorder(elBorderLabel);
		lbl_precio.setBorder(elBorderLabel);
		add(lbl_nombre);
		add(lbl_cant);
		add(lbl_precio);
		add(new NullField(Field.FOCUSABLE));
	}
	protected void sublayout1(int maxWidth, int maxHeight) 
	{
		int height = 40;
	    super.sublayout(Display.getWidth(), height);
	    setExtent(Display.getWidth(), height);
	}
	public void sublayout( int width, int height ) 
	{

	      /*int _targetHeight = getFont().getHeight() / 2 * 3 + 2 * VPADDING;
	//#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
	      if( Touchscreen.isSupported() ) {
	          _targetHeight = getFont().getHeight() * 2 + 2 * VPADDING;
	      }
	//#endif
	      */
			super.sublayout(width, height);
			setExtent( WIDTH_HFM, 
					_targetheight);
			
			//invalidate();
	  }
	  
	protected void paint(Graphics g) 
	{
	    if (_isFocus)
	    {
			g.setBackgroundColor(COLOR_BACKGROUND_FOCUS_ACTION);	
	    }
	    else
	    {
	    	g.setBackgroundColor(COLOR_BACKGROUND);
	    }
	    g.clear();
	    super.paint(g);
	}
	protected void onFocus(int direction) 
	{
	    super.onFocus(direction);
	    _isFocus = true;
	    invalidate();
	}
	protected void onUnfocus() 
	{
	    super.onUnfocus();
	    _isFocus = false;
	    invalidate();
	}
	protected boolean navigationClick(int status, int time) 
	{
	
	    if(Touchscreen.isSupported())
	    {
	        return false;
	    }
	    else
	    {
	        fieldChangeNotify(1);
	        return true;
	    }
	
	}
	protected boolean touchEvent(TouchEvent message)
	{
	    if (TouchEvent.CLICK == message.getEvent())
	    {
	        FieldChangeListener listener = getChangeListener();
	        if (null != listener)
	        {
	            this.setFocus();
	        }
	        listener.fieldChanged(this, 1);
	    }
	    return super.touchEvent(message);
	}
    
    




}
